<div class="layui-body">
    <div style="padding: 16px;">
        <div class="layui-bg-gray" style="padding: 16px;">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md6">
                    <div class="layui-card">
                        <div class="layui-card-header">知识库信息</div>
                        <div class="layui-card-body">
                            当前知识库内容量为：<a id="zhishiku"></a> 条
                        </div>
                    </div>
                </div>
                <div class="layui-col-md6">
                    <div class="layui-card">
                        <div class="layui-card-header">用户信息</div>
                        <div class="layui-card-body">
                            当前用户量为：<a id="yonghuliang"></a> 位
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-bg-gray" style="padding: 16px;">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md6">
                    <div class="layui-card">
                        <div class="layui-card-header">数据库备份</div>
                        <div class="layui-card-body">
                            <button type="button" class="layui-btn layui-btn-radius" id="backup">开始备份</button>
                        </div>
                    </div>
                </div>
                <div class="layui-col-md6">
                    <div class="layui-card">
                        <div class="layui-card-header">数据库还原</div>
                        <div class="layui-card-body">
                            <select id="backupList" class="layui-select" style="width: 100%;">
                                <!--备份文件列表将会填充到这里-->
                            </select>
                            <button id="restoreButton" type="button" class="layui-btn layui-btn-danger layui-btn-radius">开始还原</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-bg-gray" style="padding: 16px;">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md6">
                    <div class="layui-card">
                        <div class="layui-card-header">无用文件清理</div>
                        <div class="layui-card-body">
                            <button type="button" class="layui-btn layui-btn-radius">开始清理</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    var c1 = document.getElementById("zhishiku");
    var c2 = document.getElementById("yonghuliang");
    fetch('/admin/api/sysfrom.php', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            body: 'username=<?php echo $_SESSION['username']; ?>'
        })
        .then(response => response.json())
        .then(data => {
            // console.log(data);
            c1.innerHTML = data.count1;
            c2.innerHTML = data.count2;
        })
        .catch(error => {
            console.log(error);
        });
    // 监听按钮点击事件
    document.getElementById("backup").addEventListener("click", function() {
        // 备份数据库请求
        fetch('./api/backupsql.php')
            .then(response => {
                if (response.ok) {
                    return response.text();
                } else {
                    throw new Error('备份失败');
                }
            })
            .then(data => {
                alert(data); // 输出备份成功提示
            })
            .catch(error => {
                alert('备份请求出错');
            });
    });

    // 页面加载完毕后执行
    window.onload = function() {
        // 发送请求获取备份文件列表
        fetchBackupList();
    };

    // 获取备份文件列表
    function fetchBackupList() {
        fetch('./api/get_backup_list.php')
            .then(response => {
                if (response.ok) {
                    return response.json();
                } else {
                    throw new Error('获取备份列表失败');
                }
            })
            .then(data => {
                // 将备份文件列表填充到下拉框中
                const backupListSelect = document.getElementById('backupList');
                backupListSelect.innerHTML = '';
                data.forEach(backup => {
                    const option = document.createElement('option');
                    option.value = backup.filename;
                    option.text = backup.filename;
                    backupListSelect.appendChild(option);
                });
            })
            .catch(error => {
                console.error('获取备份列表失败:', error);
            });
    }

    // 开始还原按钮点击事件处理
    document.getElementById('restoreButton').addEventListener('click', function() {
        // 获取选择的备份文件名
        const selectedBackup = document.getElementById('backupList').value;
        // 发送请求开始数据库还原
        restoreDatabase(selectedBackup);
    });

    // 发起数据库还原请求
    function restoreDatabase(backupFileName) {
        // 发送还原请求
        const formData = new URLSearchParams();
        formData.append('backup_file', backupFileName);

        fetch('./api/restore_database.php', {
            method: 'POST',
            body: formData
        })
            .then(response => {
                if (response.ok) {
                    return response.json();
                } else {
                    throw new Error('数据库还原失败');
                }
            })
            .then(data => {
                if (data.success) {
                    alert('数据库还原成功');
                } else {
                    throw new Error(data.message);
                }
            })
            .catch(error => {
                console.error('数据库还原失败:', error);
                alert('数据库还原失败: ' + error.message);
            });
    }
</script>